/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.techshop.dao.impl;

import com.techshop.dao.OrderDetailDao;
import com.techshop.model.OrderDetail;
import java.math.BigDecimal;
import java.util.List;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 *
 * @author Hai-PC
 */
public class OrderDetailDaoImpl extends HibernateDaoSupport implements OrderDetailDao
{
    @Override
    public OrderDetail getById(int deliveryNoteDetailsId)
    {
        try
        {
            return (OrderDetail) getSession().get(OrderDetail.class, deliveryNoteDetailsId);
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
            return null;
        }
    }

    @Override
    public List<OrderDetail> getByDeliveryNoteId(int deliveryNoteId)
    {
        try
        {
            return getSession().createQuery("FROM OrderDetail AS d WHERE d.order.id=:deliveryNoteId").setInteger("deliveryNoteId", deliveryNoteId).list();
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
            return null;
        }
    }
    
    @Override
    public int getOrderQuantity(String storeId, String productId, String colorId)
    {
        try
        {
            String sql = "SELECT SUM(dd.Quantity) FROM DeliveryNote d, DeliveryNoteDetails dd, Schedule s WHERE d.DeliveryNoteId = dd.DeliveryNoteId AND dd.ProductId = :productId AND dd.ColorId = :colorId AND d.IsDelivered = 0 AND d.SellerScheduleId = s.ScheduleId AND s.StoreId = :storeId";
            BigDecimal results = (BigDecimal)getSession().createSQLQuery(sql).setParameter("storeId", storeId).setParameter("productId", productId).setParameter("colorId", colorId).list().get(0);
            return results.intValue();
        }
        catch (Exception e)
        {
            System.err.printf("Error: ", e.toString());
            return 0;
        }
    }
    
    @Override
    public List<OrderDetail> findAll()
    {
        try
        {
            return getSession().createQuery("FROM OrderDetail").list();
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
            return null;
        }
    }

    @Override
    public boolean insert(OrderDetail deliveryNoteDetails)
    {
        try
        {
            if (getById(deliveryNoteDetails.getId()) != null)
            {
                return false;
            }
            else
            {
                getHibernateTemplate().save(deliveryNoteDetails);
                return true;
            }
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
            return false;
        }
    }
    
    @Override
    public boolean update(OrderDetail deliveryNoteDetails)
    {
        try
        {
            if (getById(deliveryNoteDetails.getId()) == null)
            {
                return false;
            }
            else
            {
                getHibernateTemplate().update(getHibernateTemplate().merge(deliveryNoteDetails));
                return true;
            }
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
            return false;
        }
    }

    @Override
    public boolean delete(OrderDetail deliveryNoteDetails)
    {
        try
        {
            if (getById(deliveryNoteDetails.getId()) == null)
            {
                return false;
            }
            else
            {
                getHibernateTemplate().delete(getHibernateTemplate().merge(deliveryNoteDetails));
                return true;
            }
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
            return false;
        }
    }
    
    @Override
    public boolean updateAll(List<OrderDetail> deliveryNoteDetailsList)
    {
        try
        {
            getHibernateTemplate().saveOrUpdateAll(deliveryNoteDetailsList);
            return true;
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
            return false;
        }
    }
}